package com.eui.arch.wizard.model
{
	import com.eui.arch.application.model.NodePM;
	import com.eui.arch.util.ManagedPM;
	import com.eui.arch.wizard.event.StepNavEvent;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.rpc.AsyncResponder;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	[Bindable]
	public class StepPM extends ManagedPM implements IDataManaging
	{
		public var errorMessage:String;
		public var warnMessage:String;
		
		public function StepPM()
		{
		}

		public function enterStep(data:Object, responder:IResponder):void
		{
			responder.result(null);
		}

		//Check data change
		public function exitStep(responder:IResponder):void
		{
			validateUIData(new AsyncResponder(validateSuccess, validateFail));
			function validateSuccess(result:Object, token:Object = null):void{
				trace("validateUIData in stepPM " + pmId() + " success!");
				if (isDataChanged())
				{
					//This is default Endurance behaviour, can be replaced by alert and revert
//					saveChange(responder);
					Alert.yesLabel = "Yes";
					Alert.noLabel = "No";
					Alert.show("You have unsaved changes. Do you want to continue?", "Unsaved Changes", 3, null, alertClickHandler);
					function alertClickHandler(event:CloseEvent):void{
						if (event.detail == Alert.YES)
						{
							revertChange(responder);
						}
						else
						{
							responder.fault(null);
						}
					}
				} else{
					responder.result(null);
				}
			}
			function validateFail(error:FaultEvent, token:Object = null):void
			{
				trace("validateUIData in stepPM " + pmId() + " fail!");
				//Block navigation, stay in the step while validateUIDate fail
				responder.fault(null);
			}
		}


		public function validateUIData(responder:IResponder):void
		{
			throw new Error("method validateUIData() in StepPM must be overriden by child class");
		}

		public function isDataChanged():Boolean
		{
			throw new Error("method isDataChanged() in StepPM must be overriden by child class");
		}

		public function saveChange(responder:IResponder):void
		{
			throw new Error("method save() in StepPM must be overriden by child class");
		}

		public function revertChange(responder:IResponder):void
		{
			throw new Error("method revert() in StepPM must be overriden by child class");
		}
	}
}